const Page = require('../page');
const G6 = require('../../../../../g6/src');
const { Util } = G6;

Page.registerBehaviour('zoomCanvas', page => {
  const graph = page.getGraph();
  let timeout;
  graph.behaviourOn('wheel', ev => {
    const { domEvent } = ev;
    domEvent.preventDefault();
  });
  graph.behaviourOn('wheel', Util.throttle(ev => {
    if (page.getSignal('preventWheelPan')) {
      return;
    }
    const { domEvent } = ev;
    const wheelZoom = page.getSignal('wheelZoom');
    if (!timeout) {
      page.setCapture(false);
    }
    const delta = domEvent.wheelDelta;
    const times = 1.05;
    if (Math.abs(delta) > 10) { // 控制灵敏度
      const originScale = graph.getMatrix()[0];
      if (delta > 0) {
        graph.zoom({
          x: ev.x,
          y: ev.y
        }, originScale * times);
      } else {
        graph.zoom({
          x: ev.x,
          y: ev.y
        }, originScale * (1 / times));
      }
    }
    timeout && clearTimeout(timeout);
    timeout = setTimeout(() => {
      page.setCapture(true);
      timeout = undefined;
    }, 50);
  }, 16));

});
